473,526 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select Box Validation

I am having issues with making sure users select an option from a select
box... here is my code

function checkit(){
if (document.postdata.phonenumber == ""){
alert("Please enter a Phone Number");
return false;}
if (document.postdata.request[document.postdata.request.selectedIndex].value
== ""){
alert("Please Select a Request");
return false;}
}


<form name=postdata action=?action=add method=post onsubmit='return
checkit()'>
<input type=text name=phonenumber>
<select name=request>
<option value=1>Send manual</option>
<option value=2>Duplicate Product</option>
<option value=3>Did not order</option>
</select>
<input type=submit>
</form>

can someone help me figure this out. I believe it is because
document.postdata.request is undefined until an option is chosen but I don't
know how to fix that.

Jul 20 '05 #1
2 22958
Brian

Check for a selectedIndex value of -1. If no item is selected in a
Select object then:
document.formName.selectObjectName.selectedIndex
will return -1.

Hope that helps

Jul 20 '05 #2
Lee
Brian Kramer said:

I am having issues with making sure users select an option from a select
box... here is my code

function checkit(){
if (document.postdata.phonenumber == ""){
alert("Please enter a Phone Number");
return false;}
if (document.postdata.request[document.postdata.request.selectedIndex].value
== ""){
alert("Please Select a Request");
return false;}
}
can someone help me figure this out. I believe it is because
document.postdata.request is undefined until an option is chosen but I don't
know how to fix that.


No, the problem is that the value of selectedIndex is -1 until an option
is chosen. and document.postdata.request[-1].value is undefined.
That should be all you need to know to fix it.

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4954
by: Phil Powell | last post by:
Has anyone here ever done a case where you have a select multiple form element and you have to do both server-side and client-side validation? I am honestly not sure how to do it in Javascript (I keep getting errors thrown that I can't verify because the form processes onto itself too quickly for me to check the Javascript errors) because...
1
2616
by: Griff | last post by:
Hi! I'm using JavaScript on an HTML page for form validation. I have a form value (theForm.ProdType1) which represents a picklist from which users may make multiple selections; essentially, this is a Select object with the "multiple" attribute. There are 20 options to choose from in this object; one of the options (we'll call it option X)...
7
2010
by: | last post by:
I can't get IE 6 to read the values in my <SELECT..> data entry fields. Netscape 7 and Opera see them, and IE will pass the values to the database, but the javascript validation script gets a null value from the selection fields. Text inputs work just fine. Below is the form and validation script (I've added "...." to indicate additional...
4
4351
by: Alonso | last post by:
Hello everybody! :-D I am trying to validate a select object in a form. It does not work fine in Explorer 6.0 because it always send the form, but in Netscape 7.0 it works really fine. The code is below: //---------------------------------------------------------------------- <head> <script > function validate()
5
1409
by: Tim Johnson | last post by:
Howdy: I'm just learning how to use focus() and select(). I have a very large form (360+ elements). Javascript validation is launched by the 'onclick' event handler in the "submit" button tag. Problem: When the element to be validated is not on the screen - since the form is larger than the screen, then there are no visual cues to help
5
2123
by: Allan M. | last post by:
I have a series of select boxes that must be populated client side, because they interact with each other. The design specification calls for these boxes to be updated without having to make a roundtrip to the server. The codebehind seems to be unaware of select box members populated via javascript. So, I'm having to create my own state...
0
966
by: BijuP | last post by:
Hi, I have a datagird having datatable as its datasource. It has a column called "Check" which appears as checkbox in the grid. The user can manually go and click on that field to check/uncheck it, and also the user has the facility to check all the checkboxes with a click of a button. To achieve this I am doing this.. On Button_Click...
2
2007
by: Gary Dale | last post by:
I have a form with a pull-down list with six options, each of which has a value set. The value is the e-mail account name (without the domain) of a group while the displayed value is the full name of the group that will receive the e-mail. I pass this.form to a function to validate the other data before handing it off to a script to...
5
1772
by: Bubba | last post by:
I have a dynamic pulldown list (ASP with javascript) that when a user picks a state, the corresponding counties for that state appear in a dynamic second pulldown list. When I submit the form, the values for state and county are numerals 1 (for the state) and 2 (for the county selected) and not the values for the state and county ???? any...
2
1719
by: Sudhakar | last post by:
i have two select tags as part of a registration form, city1 city2 where city1 has a list of regions and similar for city2 there are different regions for city1 and city2 so instead of all the regions appearing one after the other i would like to create a blank option followed by the next set of regions for formatting purpose only. ex=...
0
7329
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7253
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7604
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5782
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1703
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
551
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.